﻿// Shader-Based-Image-Processing (SBIP)
// http://code.google.com/p/sbip/
//
// Copyright © Frank Nagl, 2009-2010
// admin@franknagl.de
//
namespace SBIP.Filter
{
    using Microsoft.Xna.Framework.Graphics;
    using System;

    /// <summary>
    /// Extract RGB channel from image.
    /// </summary>
    /// 
    /// <remarks><para>Extracts specified channel of color image and returns
    /// it as grayscale image.</para>
    /// 
    /// <para>Sample usage:</para>
    /// <code>
    ///  // 1. Offline rendering
    ///  // create Processor, used as rendering framework
    ///  Processor processor = new Processor();
    ///  // starts Processor
    ///  processor.Begin( image );
    ///  // create ExtractChannel filter
    ///  ExtractChannel filter = new ExtractChannel( );
    ///  processor.Filter = filter;
    ///  // optional: configure filter
    ///  filter.Channel = RGB.R;
    ///  // apply the filter  
    ///  Bitmap resultImage = processor.RenderToBitmap( );
    ///  Texture2D resultTexture = processor.RenderToTexture( );
    ///  processor.End( );
    ///  
    ///  // 2. Online rendering
    ///  // create any windows control for rendering in
    ///  Form myForm = new Form( );
    ///  // create Processor, used as rendering framework
    ///  Processor processor2 = new Processor( );
    ///  // starts Processor
    ///  processor2.Begin( image, myForm );
    ///  // create ExtractChannel filter
    ///  ExtractChannel filter2 = new ExtractChannel( );
    ///  processor2.Filter = filter2;  
    ///  // optional: configure filter
    ///  filter2.Channel = RGB.R;
    ///  // apply the filter
    ///  myForm.Show( );
    ///  while ( myForm.Created )
    ///  {
    ///      processor2.Render( );
    ///      Application.DoEvents( );
    ///  }
    ///  processor2.End( );
    /// </code>
    /// 
    /// <para><b>Initial image:</b></para>
    /// <img src="img/shaderbased/sample1.jpg" width="480" height="361" />
    /// <para><b>Result image:</b></para>
    /// <img src="img/shaderbased/ExtractChannel.jpg" width="480" height="361" />
    /// </remarks>
    public sealed class ExtractChannel : BaseFilter
    {
        private short channel;       
        /// <summary>RGB channel to replace.</summary>
        /// <remarks>
        /// <para>Default value is set to <see cref="SBIP.RGB.B"/>.</para>
        /// </remarks>
        /// <exception cref="ArgumentException">Invalid channel is specified.
        /// </exception>
        public short Channel
        {
            get { return channel; }
            set 
            {
                if (value != RGB.R && value != RGB.G && value != RGB.B)
                    throw new ArgumentException("Invalid channel is specified.");

                if (Effect == null)
                    throw new ArgumentException("Channel" + InitMsg);

                channel = value;
                Effect.Parameters["Channel"].SetValue(channel);
            }
        }      
        
        /// <summary>
        /// Initializes a new instance of the <see cref="ReplaceChannel"/> class.
        /// </summary>
        public ExtractChannel()
            : base("ExtractChannel") { }
    }
}
